<!--
 * @Descripttion: ''
 * @version: 1.0.0
 * @Author: CaiLuLu
 * @Date: 2021-10-25 13:48:32
 * @LastEditors: Andy
 * @LastEditTime: 2021-11-30 10:29:56
-->
<template>
  <div>
    <a-radio-group :value="value" @change="handleChange" :disabled="disabled">
      <a-radio v-for="dict in data" :key="dict.itemCode" :value="dict.itemCode" :style="radioStyle">
         <slot :name="dict.itemCode" :option="{code: dict.itemCode, desc: getDesc(dict)}">
          {{ getDesc(dict) }}
        </slot>
      </a-radio>
    </a-radio-group>
  </div>
</template>
<script>
export default {
  created() {
    this.fetch()
  },
  props: {
    value: { type: [String, Boolean] },
    filters: { type: [Array], default: () => { return [] } },
    include: { type: [Boolean], default: true },
    dictCode: { type: [String] },
    sortOrder: { type: [String], default: 'asc' },
    disabled: { type: [Boolean] },
    i18n: { type: [Boolean], default: true },
    active: { type: [Boolean], default: true },
    radioStyle: {type: Object, default: () => { return {}} }
  },
  data() {
    return {
      data: [],
      locale: localStorage.locale,
    }
  },
  methods: {
    getDesc(dict) {
      let label = dict.itemName
      if (this.i18n) {
        label = dict.itemNameI18n[this.locale]
      }
      return label
    },
    fetch () {
      let data = this.$store.state.dicts[this.dictCode]
      if (!data) {
        this.data = []
        return
      }
      this.data = data.filter(item => {
        if (this.active) {
          if (!item.isActive) {
            return false
          }
        }
        if (this.filters.length == 0) {
          return true
        }
        if (this.filters.includes(item.itemCode)) {
          if (this.include) {
            return true
          }
        } else {
          if (!this.include) {
            return true
          }
        }
        return false
      })

      if (this.dictCode == 'TRUE_FALSE_IND') {
        this.data = this.data.map(dict => {
          return {
            itemCode: dict.itemCode == "true"? true :false,
            itemName: dict.itemName,
            itemNameI18n: dict.itemNameI18n,
            orderNo: dict.orderNo
          }
        })
      }
      if (this.sortOrder.toUpperCase() == 'DESC') {
        this.data = this.data.map(dict => { return dict })
        this.data.sort((a,b) => {
          return b.orderNo - a.orderNo
        })
      }
    },
    handleChange(e) {
      this.$emit('change', e.target.value);
    },
  }
}
</script>
